<template>
  <div class="app-container">
      <el-form ref="subscriberForm" :rules="rules" :model="subscriberForm" label-position="left" label-width="70px" style="width: 400px; margin-left:50px;">

        <el-form-item label="用户名" prop="username">
          <el-input v-model="subscriberForm.username" />
        </el-form-item>
        <el-form-item label="全名" prop="fullname">
          <el-input v-model="subscriberForm.fullname" />
        </el-form-item>
        <el-form-item label="角色" prop="roleID">
          <el-select v-model="subscriberForm.roleID" class="filter-item" placeholder="选择学校">
            <el-option v-for="item in roleOptions" :key="item.ID" :label="item.title" :value="item.ID" />
          </el-select>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="subscriberForm.password" />
        </el-form-item>
        <el-form-item label="确认密码" prop="confirm">
          <el-input v-model="subscriberForm.confirm" />
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="subscriberForm.remark" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" placeholder="备注" />
        </el-form-item>
      </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCannel()">
        取消
      </el-button>
      <el-button type="primary" @click="saveSubscriber()">
        确认
      </el-button>
      <el-button v-if="subscriberForm.status.value === 5" type="danger" @click="handleDelete(subscriberForm)">
        删除
      </el-button>
    </div>
  </div>
</template>

<script>
import {  saveSubscriber, getSubscriberDetail, removeSubscriberByID } from '@/api/authority/subscriber'
import {  getRoleUseable } from '@/api/authority/role'


import waves from '@/directive/waves' // waves directive
import { parseTime } from '@/utils'

import Pagination from '@/components/Pagination' // secondary package based on el-pagination

export default {
  name: 'ComplexTable',
  components: { Pagination },
  directives: { waves },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'info',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      roleOptions:[],
      listQuery: {
        start: 1,
        perpage: 10,
        title: undefined,
        sort: '+id'
      },
      showReviewer: false,
      subscriberForm: {
        identity: undefined,
        remark: '',
        subscriber_identity: 0,
        title: '',
        status: {
          value:1
        }
      },
      rules: {
        type: [{ required: true, message: 'type is required', trigger: 'change' }],
        timestamp: [{ type: 'date', required: true, message: 'timestamp is required', trigger: 'change' }],
        title: [{ required: true, message: 'title is required', trigger: 'blur' }]
      },
      downloadLoading: false
    }
  },
  created() {
    this.loadRoleList()
    let subscriberID = this.$route.query && this.$route.query.subscriberID
    if (subscriberID !== undefined) {
      subscriberID = parseInt(subscriberID)
      if (!isNaN(subscriberID)) {
        this.listLoading = true
        getSubscriberDetail({
          "subscriberID":subscriberID
        }).then(response => {
          if (response.status === 200) {
            this.subscriberForm = response.data
            this.subscriberForm.password = ''
            console.log(this.subscriberForm)
          } else {
            this.$message({
              message: response.msg,
              type: 'error'
            })
          }
        })
      }
    }
  },
  methods: {
    loadRoleList() {
      getRoleUseable().then(response => {
        if(response.status === 200){
          this.roleOptions = response.data.list
        }
      })
    },
    handleCannel() {
      this.$router.push({ path: 'subscriber' })
    },
    saveSubscriber() {
      let _this = this
      this.$refs['subscriberForm'].validate((valid) => {
        if (valid) {
          _this.subscriberForm.status = _this.subscriberForm.status.value;
          if(_this.subscriberForm.hasOwnProperty('ID')){
            if(_this.subscriberForm.ID !== undefined){
              _this.subscriberForm.subscriberID = _this.subscriberForm.ID;
            }

          }
          saveSubscriber(_this.subscriberForm).then((response) => {
          if (response.status === 200) {
            this.$message({
              message: '操作Success',
              type: 'success'
            })
            this.$router.push({ path: 'subscriber' })
          } else {
            this.$message({
              message: response.msg,
              type: 'error'
            })
          }
          })
        }
      })
    },
    handleDelete(row, index) {
      removeSubscriberByID(row.ID).then(() => {
        this.$notify({
          title: 'Success',
          message: 'Update Successfully',
          type: 'success',
          duration: 2000
        })
        this.getList()
      })
    }
  }
}
</script>
